package pers.lan.stack;

import java.util.Stack;

/**
 * @author <p><a href="mailto:1728209643@qq.com">XiaoSan</a>
 * @date 2019/9/3 10:05 AM
 */
public class LC_844 {
    public static void main(String[] args) {
        System.out.println(
                new LC_844().backspaceCompare(
                        "ab#c",
                        "ad#c"
                )
        );
    }

    public boolean backspaceCompare(String S, String T) {
        return toString(S).equals(toString(T));
    }

    private String toString(String s) {
        Stack<Character> stack = new Stack<>();
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '#') {
                if (stack.empty()) {
                    continue;
                }
                stack.pop();
            } else {
                stack.push(chars[i]);
            }
        }
        return stack.toString();
    }
}
